﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FMSpaceShooter.Screens;
using Microsoft.Xna.Framework;
using FMSpaceShooter.GameObjects.ItemFolder;
using Microsoft.Xna.Framework.Graphics;

namespace FMSpaceShooter.GameObjects.EnemyFolder
{
    class TeleporterEnemy :EnemyBase
    {

        public new const string TypeName = "TeleporterEnemy";
        public const string imageName = "Images/Enemies/TeleporterEnemy";
        private double time;
        private float newRotation;
        private Vector2 newPosition;
        private bool isSelected = false;
        public override int Points { get { return 30; } }

        public TeleporterEnemy(GameplayScreen screen, Vector2 position)
            :base(screen, imageName, position, 50)
        {
            time = 0;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (screen.GOM.PlayerOne.IsInPlay)
            {
                time-= gameTime.ElapsedGameTime.Milliseconds;
                if (time < 1)
                {
                    position= newPosition;
                    rotation= newRotation;
                    time = screen.Random.Next(5000)+1000;
                    isSelected = false;
                }
                if (time<1000 && !isSelected )
                {
                    newPosition = new Vector2(screen.Random.Next(screen.GraphicsDeviceManager.PreferredBackBufferWidth), screen.Random.Next(screen.GraphicsDeviceManager.PreferredBackBufferHeight));
                    newRotation = (float)Math.Atan(Vector2.Distance(screen.GOM.PlayerOne.Position,newPosition));
                    isSelected = true;
                }

                var direction = screen.GOM.PlayerOne.Position - position;
                direction.Normalize();
                rotation = Util.Tools.VectorToAngle(direction);
            }
            else
            {
                rotation += (float)(1f * gameTime.ElapsedGameTime.TotalSeconds);
            }
        }

        public override void Die()
        {
            base.Die();
            int x = screen.Random.Next(20);

            if (x < 3)
            {
                screen.GOM.AddToPlay(new PrimaryBulletBoost(screen, this.position));
            }
            else if (x > 17)
            {
                screen.GOM.AddToPlay(new SplitterBulletBoost(screen, this.position));
            }
            else if (x == 5)
            {
                screen.GOM.AddToPlay(new ExplosionBulletBoost(screen, this.position));
            }

        }


        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            SpriteBatch spriteBatch = screen.ScreenManager.SpriteBatch;

            if (isSelected)
            {
                spriteBatch.Draw(screen.GOM.GetTexture("Images/Enemies/TeleporterImage"), newPosition, null, Color.White,
                newRotation, origin, scale, SpriteEffects.None, 0);

            }
            
        }


    }
}
